<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Item.php");
require_once("../../data/Comment.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/Hidden.php");
require_once("HTML/QuickForm/Select.php");
require_once("HTML/QuickForm/Submit.php");

/**
 * Action allocation page.<br/>
 * Form to be used by a user to allocate an action to another user.
 */
class AllocateActionPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
		
		$l_itemInput=new HTML_QuickForm_hidden("itemId");
		$this->addElement($l_itemInput);
		
		$l_allocatedUserSelect=new HTML_QuickForm_select("allocatedUser");
		$l_allocatedUserSelect->setLabel("Allocate to:");
		$l_records=User::getFromCustomCriteria(
			"WHERE isActive=:isActive and not id=:id",
			array("isActive" => true, "id" => SessionManager::getCurrentLoggedUserId()));
		$l_allocatedUserSelect->addOption("No one","null");
		foreach($l_records as $l_user){
			$l_allocatedUserSelect->addOption($l_user->getFullName(),base64_encode($l_user->getId()));
		}
		$this->addElement($l_allocatedUserSelect);
        
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		$l_cancel=new HTML_QuickForm_submit($this->getButtonName("cancel"));
		$l_cancel->setValue("Cancel");
		$this->addElement($l_cancel);
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	displayTop("Close the action",null,null);
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	$l_allocatedUserId=$page->exportValue("allocatedUser");
        if($l_allocatedUserId!="null"){
        	//Actions can be allocated only by the creator of the action
	    	$l_creatingUserId=$l_action->getCreatingUser_id();
			$l_me=SessionManager::getCurrentLoggedUserId();
			if($l_creatingUserId==$l_me){
	        	$l_action=Item::getFromPrimaryKey(base64_decode($page->exportValue("itemId")));
	    		$l_action->setAllocatedUser_id(base64_decode($l_allocatedUserId));
	    		$l_action->setAllocated(date("Y/m/d", time()));
	    		$l_action->save();
			}
    	}
    	
    	HTTP::redirect("index.php");
    }
}

class CancelProcess extends HTML_QuickForm_Action{
    function perform($page){
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('allocateActionProcess', false);
$controller->container(true);
$controller->addPage(new AllocateActionPage('actionAllocationPage'));
$controller->setDefaults(array(
		"itemId" => $_REQUEST["id"]));
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('cancel', new CancelProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>